package com.quectel.cms.controller.datav;

import cn.hutool.core.date.DateUtil;
import com.quectel.cms.controller.BaseController;
import com.quectel.cms.vo.datav.AiControlAiEventVo;
import com.quectel.core.module.ai.service.mongo.MongoAiEventRecordService;
import com.quectel.util.common.DateUtils;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * @author rananxin
 * @date 2021/12/7 4:00 下午
 */
@RestController
@RequestMapping("datav/aiControl")
@Tag(name = "DataVAiControlController", description = "大屏AI防控相关接口")
public class DataVAiControlController extends BaseController {

    @DubboReference
    private MongoAiEventRecordService aiEventRecordService;

    @GetMapping("aiEvent")
    @Operation(summary = "通过小区id,查询ai事件列表")
    public Response<AiControlAiEventVo> aiEvent(
            @Parameter(description = "小区id 非必填") @RequestParam Long villageId
    ) {
        long currentTimeMillis = System.currentTimeMillis();

        AiControlAiEventVo result = new AiControlAiEventVo();

        Map<String, Object> params = Request.parseParams(httpServletRequest);
        super.fillAuthorityData(params)
                .put("endTime", DateUtils.format(new Date(currentTimeMillis), DateUtils.FormatType.COMMON));

        params.put("startTime", DateUtils.format(DateUtil.beginOfDay(new Date(currentTimeMillis)), DateUtils.FormatType.COMMON));
        int totalDay = aiEventRecordService.queryTotal(params);

        params.put("startTime", DateUtils.format(DateUtil.beginOfWeek(new Date(currentTimeMillis)), DateUtils.FormatType.COMMON));
        int totalWeek = aiEventRecordService.queryTotal(params);

        params.put("startTime", DateUtils.format(DateUtil.beginOfMonth(new Date(currentTimeMillis)), DateUtils.FormatType.COMMON));
        int totalMonth = aiEventRecordService.queryTotal(params);

        result.setEventCountToday(totalDay);
        result.setEventCountWeek(totalWeek);
        result.setEventCountMonth(totalMonth);

        return Response.<AiControlAiEventVo>ok().wrap(result);
    }
}
